<%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2018/6/1
  Time: 11:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
    /*
    * 上传文件
    * */
    function clickFile() {
        var file=document.getElementById("file");
        file.click();
    }
    function clickDirectory() {
        var dir=document.getElementById("directory");
        dir.click();
    }
    var file;
    var directory = [];
    function upload(input) {
        file=input.files[0];
        if(file==null)
            return;
        uploadFile();
    }
    function uploads(input) {
        directory=input.files;
        if(directory==null)
            return;
            uploadDirectory();
    }
    function check() {
        if(file!=null)
            alert(file.name);
        else {
            for (var i = 0; i < directory.length; i++)
                alert(directory[i].webkitRelativePath);
        }
    }
    function uploadFile() {
        var fd=new FormData();
        var id=getQueryString("folderId");
        fd.append("file",file);
        fd.append("parent",id);
        $.ajax({
            url: "../files/uploadFile",
            type: "post",
            data: fd,
            contentType: false,
            processData: false,
            cache: false,
            success: function (data) {
                alert("上传成功");
                window.location.reload();
            },
            error: function () {
                alert("操作失败")
            }
        })
    }
    function uploadDirectory() {
        var fd = new FormData();
        var id=getQueryString("folderId");
        for (var i = 0; i < directory.length; i++) {
            fd.append("files", directory[i]);
            fd.append("paths",directory[i].webkitRelativePath);
        }
        fd.append("parent",id);
        //fd.append("parent","1234567");
        $.ajax({
            url: "../files/uploadFiles",
            type: "post",
            data: fd,
            contentType: false,
            processData: false,
            cache: false,
            success: function (data) {
                if(data=="success"){
                    alert("上传成功");
                    window.location.reload();
                }
                else
                    alert(data);
            },
            error: function () {
                alert("操作失败")
            }
        })
    }
</script>
<div class="dropdown" style="background: #D0EEFF;border: 1px solid #99D3F5;border-radius: 4px;padding: 4px 12px;line-height: 30px;">
    <a class="dropdown-toggle" id="user" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
        上传文件 <span class="caret"></span></a>
    <ul class="dropdown-menu">
        <input id="file" type="file" onchange="upload(this)" style="display: none">
        <li><a onclick="clickFile()" style="cursor:pointer">上传文件</a></li>
        <input id="directory" type="file" onchange="uploads(this)" webkitdirectory style="display: none">
        <li><a onclick="clickDirectory()" style="cursor:pointer">上传文件夹</a></li>
    </ul></div>